<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class MsgVerifyModel{ 
    protected $CI;
    public function __construct(){
        $this->CI =& get_instance();
        $this->CI->load->database();
    }
    /**
     * [userLoginByAccount description]
     * @param  [type] $account  [description]
     * @param  [type] $datenow [description]
     * @return [type]           [description]
     */
    public function countByAccount($account,$datenow){
        $query = "SELECT count(id) as total FROM user_msg_verify
                 where account = '$account' and datanow = '$datenow' limit 1";
        // echo $query;exit;
        $info = $this->CI->db->query($query);
        $object = $info->row();
        if(isset($object->total)){
            //账号
            return $object->total;
        }
        else{
            return 0;
        }
    }

    /**
     * [repeatMsg description]
     * @param  [type] $account [description]
     * @param  [type] $datenow [description]
     * @return [type]          [description]
     */
    public function repeatMsg($account,$datenow){
        $timeNow = date("Y-m-d H:i:s");
        $query = "SELECT id,code FROM user_msg_verify
                 where account = '$account' and datanow = '$datenow' and state = 0 and repeat_time>'$timeNow' 
                 order by id desc  limit 1";
        $info = $this->CI->db->query($query);
        $object = $info->row();
        if(isset($object->id)){
            $ret['status']  = 0;
            $ret['code']  = $object->code;
        }
        else{
            $ret['status'] = -1;
        }
        return $ret;
    }

      /**
     * [insertOneMsg description]
     * @param  [type] $account  [description]
     * @return [type]           [description]
     */
    public function insertOneMsg($account,$code,$checkMinute,$repeatMinute,$platform){
        log_message('debug', "insertOneMsg,$account:$account,$code,$checkMinute,$repeatMinute,$platform");   
        $checkTime = date("Y-m-d H:i:s",strtotime("+$checkMinute minute"));
        $repeatTime = date("Y-m-d H:i:s",strtotime("+$repeatMinute minute"));
        $datanow = date("Y-m-d");
        $query = "insert into user_msg_verify(account,code,datanow,time,repeat_time,state,platform)
                     values('$account',$code,'$datanow','$checkTime','$repeatTime',0,$platform)";
        $info = $this->CI->db->query($query);

        $ret =  $this->CI->db->affected_rows();
        return $ret;
    
    }

    /**
     * [checkCodeByAccount description]
     * @return [type] [description]
     */
    public function checkAndUpdateCodeByAccount($account,$code){
        $query = $this->CI->db->order_by('id', 'DESC')->get_where('user_msg_verify', array('account' => $account,'code'=>$code), 1, 0);
        $result = $query->result();
        if(isset($result[0]) && $result[0]->id){
            if( $result[0]->time>date("Y-m-d H:i:s") && $result[0]->state==0){
                //有效，改成已验证的
                $status = $this->CI->db->set("state",1)->where("id",$result[0]->id)->update('user_msg_verify');
                return 0;
            }
            else{
                return 2010;
            }
        }
        else{
            return 2014;
        }
    }


}